﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Orbitone.MeetingRooms.RoomFinder.UserControls.GridViewModel;

namespace Orbitone.MeetingRooms.RoomFinder.UserControls.GridView
{
    public class GridViewWrapper
    {
        private readonly DataTable _datatable;
        private readonly System.Web.UI.WebControls.GridView _gridview;
        public EventHandler gvt_CalendarItemClick;

        public GridViewWrapper(System.Web.UI.WebControls.GridView gridview)
        {
            _gridview = gridview;
            _datatable = new DataTable();
        }

        public void AddColumn(string columnName)
        {
            _datatable.Columns.Add(columnName, typeof (IGridViewItem));
        }

        public void AddRow()
        {
            _datatable.Rows.Add(_datatable.NewRow());
        }

        public void SetLabel(int rowNr, int columnNr, string label)
        {
            _datatable.Rows[rowNr][columnNr] = new GridViewLabelItem {Label = label, Rowspan = 0};
        }

        public void SetLabel(int rowNr, int columnNr, string label, string title, int rowspan)
        {
            _datatable.Rows[rowNr][columnNr] = new GridViewLabelItem {Label = label, Title = title, Rowspan = rowspan};
        }

        public void SetLinkButton(int rowNr, int columnNr, string label, string title, string commandArgument,
                                  string commandName, int rowspan)
        {
            _datatable.Rows[rowNr][columnNr] = new GridViewLinkItem
                                                   {
                                                       Label = label,
                                                       CommandArgument = commandArgument,
                                                       CommandName = commandName,
                                                       Rowspan = rowspan,
                                                       Title = title,
                                                   };
        }

        public void SetHiddenItem(int rowNr, int columnNr)
        {
            _datatable.Rows[rowNr][columnNr] = new GridViewHiddenItem();
        }

        public void DataBind()
        {
            foreach (DataColumn col in _datatable.Columns)
            {
                var bfield = new TemplateField();
                bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
                var gvt = new GridViewTemplate(ListItemType.Item, col.ColumnName);
                gvt.CalendarItemClick += gvt_CalendarItemClick;
                bfield.ItemTemplate = gvt;
                _gridview.Columns.Add(bfield);
            }

            _gridview.RowDataBound += DataGridRowDataBound;

            _gridview.DataSource = _datatable;
            _gridview.DataBind();
        }

        protected void DataGridRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //starting from row 1, so skipping row 0 (Timestamps)
                for (int cnColNumber = 1; cnColNumber < e.Row.Cells.Count; cnColNumber++)
                {
                    TableCell cell = e.Row.Cells[cnColNumber];
                    string rowspan = ((Label) e.Row.Cells[cnColNumber].Controls[0]).Text;
                    int rowspanint;
                    if (int.TryParse(rowspan, out rowspanint))
                    {
                        if (rowspanint > 0)
                        {
                            cell.RowSpan = rowspanint;
                        }
                        else
                        {
                            //Cell is part of timespan which is overruled by rowspan
                            cell.Visible = false;
                        }
                    }
                }
            }
        }
    }
}